#!/bin/bash

function output(){
  echo "\n
----  $1  ----
\n"
}

echo -e `output "installing co2 now"`

# * Usage: $0 
#  --dir        DIRECTORY 
#  --cpu        NUMBER_OF_CORES_TO_USE
#  --php        PATH_TO_PHP_INTERPRETER
#  --with-app   "APP VERSION"
#  --channel    DISTRIB (STABLE|dev)
#  --version    VERSION
#  --api        API-VERSION
#  --api-channel API_DISTRIB (STABLE|dev)

# some system info

cpu=$(grep -c processor /proc/cpuinfo) # number of processors

#	default parameters

version=0.0.0
distrib=
here=`pwd`
current=$(dirname $(readlink -f $0))
dir=$here
php_interpreter=/usr/bin/php
channel=dev
api=0.0.0
api_channel=dev

default_apps=( "yaml 0.0.0" )

# exit status

E_BASE_DIRECTORY_COULD_NOT_BE_CREATED=1
E_BASE_DIRECTORY_NAME_TAKEN=2

# read user input
declare -a myArgs

key=0

for i
do
	myArgs[key]="$i"
	
	key=$(( $key + 1 ))
done

key=0
for k in ${myArgs[@]}
do
	next=$(( $key + 1 ))
	
	case $k in
		--dir )
			dir="${myArgs[$next]}"	
		;;
		--cpu )
			cpu="${myArgs[$next]}"
		;;
		--php )
			php_interpreter="${myArgs[$next]}"
		;;
		--channel )
			channel="${myArgs[$next]}"
		;;
		--version )
			version="${myArgs[$next]}"
		;;
		--api )
			api="${myArgs[$next]}"
		;;
		--api-channel )
			api_channel="${myArgs[$next]}"
		;;
		--with-app )
			default_apps=( "${default_apps[@]}" "${myArgs[$next]}" )
		;;
	esac

	key=$next
done


if [ "$channel" = 'stable' ]
then
  distrib=trunk
else
  distrib=branches
fi

if [ "$api_channel" = 'stable' ]
then
  api_distrib=trunk
else
  api_distrib=branches
fi


#	##########################################################	#


#	##########################################################	#

# install ubuntu dependencies

echo -e `output "installing aptitude"`

apt-get install aptitude

echo -e `output "installing debian/ubuntu/mint/etc. dependencies"`

aptitude install libcurl4-dev git-core subversion sqlite3



#	##########################################################	#


#	##########################################################	#

# create repertory

dir="$dir/co2"

echo -e `output "creating repertory $dir"`

if [ ! -d "$dir" ]
then
	mkdir=`mkdir -p $dir`
	
	if [ ! $mkdir = '' ]
	then
		echo "Failed to create directory $dir"
		exit $E_BASE_DIRECTORY_COULD_NOT_BE_CREATED
	fi
else
	echo "There is already a folder called $dir"
	exit $E_BASE_DIRECTORY_NAME_TAKEN
fi

dirs=( kernel kernel/conf kernel/api kernel/api/$api_channel lib apps storage storage/cache storage/log storage/tmp storage/db storage/db/sqlite3 )

for d in "${dirs[@]}"
do
  mkdir "$dir/$d"
  echo "created directory $dir/$d"
done

#	##########################################################	#


#	##########################################################	#

# bootstrap

echo -e `output "installing boot-strap from $distrib/os/$version/co2.php"`

cd $dir && svn export http://co2.googlecode.com/svn/$distrib/os/$version/co2.php co2.php

sed -i 's|---php---|#!'$php_interpreter'|' "$dir/co2.php"

#	##########################################################	#


#	##########################################################	#

# boot files

echo -e `output "installing boot files from $distrib/os/$version/kernel/boot"`

cd $dir/kernel && svn export http://co2.googlecode.com/svn/$distrib/os/$version/kernel/boot boot

#	##########################################################	#


#	##########################################################	#

# SQLite3

echo -e `output "installing database"`

sqlite3 "$dir/storage/db/sqlite3/conf.db" "

create table pkg_type (id INTEGER PRIMARY KEY,type TEXT);

insert into pkg_type
select null as 'id','app' as 'type'
union select null,'lib'
union select null,'api'
union select null,'pkg'
union select null,'php';

create table aptitude (id INTEGER PRIMARY KEY,type INTEGER,pkg TEXT,version TEXT,channel TEXT);

create unique index unique_package on aptitude (type,pkg,version,channel);

insert into aptitude values(null,3,'api','$api','$api_channel');

create table dependencies (id INTEGER PRIMARY KEY,drug INTEGER,junkie INTEGER);

create unique index unique_dependency on dependencies (drug,junkie);"

#	##########################################################	#


#	##########################################################	#

# sysvar

echo -e `output "installing system variables"`

echo "base=$dir
version=$version
distrib=$distrib
api=$api
api_channel=$api_channel
s3-bucket=co2-os-1982
php=$php_interpreter
lib=$dir/lib
apps=$dir/apps
db=$dir/storage/db
tmp=$dir/storage/tmp
kernel=$dir/kernel
storage=$dir/storage" > "$dir/kernel/conf/sysvar"

#	##########################################################	#


#	##########################################################	#

# API

echo -e `output "installing API version $version in distribution $api_distrib"`

cd $dir/kernel/api/$api_channel && svn export http://co2.googlecode.com/svn/$api_distrib/api/$api $api


echo -e `output "installation complete, type '$dir/co2.php' to begin to use co2, or 'ln -s $dir/co2.php /usr/bin/co2' to be able to type directly 'co2' from terminal"`

exit 0
